﻿//
// Copyright (c) 2011 Western Data Solutions (www.westerndatasolutions.com) (Armen Shimoon)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
// the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Linq;

namespace WDS.Bencode.Fluent
{
    /// <summary>
    /// Allows for adding dictionary entries using fluent-syntax though a lambda-function
    /// </summary>
    public class BencodeDictionaryPairAdder
    {
        /// <summary>
        /// Internal collection of dictionary items, in the order they are added
        /// </summary>
        private List<KeyValuePair<BencodeString, BencodeElement>> dictionaryItems;

        /// <summary>
        /// Default constructor
        /// </summary>
        public BencodeDictionaryPairAdder()
        {
            dictionaryItems = new List<KeyValuePair<BencodeString, BencodeElement>>();
        }

        /// <summary>
        /// Add a key-value-pair (use Bencode.Pair as helper)
        /// </summary>
        /// <param name="pair">A strongly typed key-value-pair</param>
        /// <returns></returns>
        public BencodeDictionaryPairAdder Add(KeyValuePair<BencodeString, BencodeElement> pair)
        {
            dictionaryItems.Add(pair);
            return this;
        }

        /// <summary>
        /// Add a key and a value
        /// </summary>
        /// <param name="key">Bencode string element</param>
        /// <param name="value">Any bencode element</param>
        /// <returns>Fluent-syntax</returns>
        public BencodeDictionaryPairAdder Add(BencodeString key, BencodeElement value)
        {
            return Add(new KeyValuePair<BencodeString, BencodeElement>(key, value));
        }

        /// <summary>
        /// Add a key and a value (using a vanilla string key)
        /// </summary>
        /// <param name="key">System.String key</param>
        /// <param name="value">Any bencode element</param>
        /// <returns>Fluent-syntax</returns>
        public BencodeDictionaryPairAdder Add(string key, BencodeElement value)
        {
            return Add(Bencode.String(key), value);
        }

        /// <summary>
        /// An enumerable list of dictionary entries in the order they were added
        /// </summary>
        /// <returns>A strongly typed list of strongly typed key-value-pairs</returns>
        public IEnumerable<KeyValuePair<BencodeString,BencodeElement>> ToEnumerable()
        {
            return dictionaryItems.ToList();
        }
    }
}